package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAdministrative;
import com.ruoyi.system.service.ISysAdministrativeService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 行政区Controller
 * 
 * @author sw
 * @date 2025-10-19
 */
@RestController
@RequestMapping("/system/administrative")
public class SysAdministrativeController extends BaseController
{
    @Autowired
    private ISysAdministrativeService sysAdministrativeService;

    /**
     * 查询行政区列表
     */
    @PreAuthorize("@ss.hasPermi('system:administrative:list')")
    @GetMapping("/list")
    public AjaxResult list(SysAdministrative sysAdministrative)
    {
        List<SysAdministrative> list = sysAdministrativeService.selectSysAdministrativeList(sysAdministrative);
        return success(list);
    }




    @PreAuthorize("@ss.hasPermi('system:administrative:list')")
    @GetMapping("/listAdministrativeByTree")
    public AjaxResult listAdministrativeByTree(SysAdministrative sysAdministrative)
    {
        List<SysAdministrative> list = sysAdministrativeService.selectSysAdministrativeList(sysAdministrative);
        list = sysAdministrativeService.listAdministrativeByTree(list);
        return success(list);
    }
    /**
     * 导入行政区
     */
    @PreAuthorize("@ss.hasPermi('system:administrative:import')")
    @Log(title = "行政区", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) throws Exception
    {
        List<Map<String, Object>> administrativeList = readExcel(file);
        return AjaxResult.success("导入成功");
    }

    /**
     * 读取excel
     * @param file
     * @return
     */
    private List<Map<String, Object>> readExcel(MultipartFile file) throws IOException {
        List<SysAdministrative> list = new ArrayList<>();
        //读取 excel
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), 0);
        List<Map<String, Object>> readAll = reader.readAll();
        return readAll;
    }

    /**
     * 获取行政区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:administrative:query')")
    @GetMapping(value = "/{administrativeId}")
    public AjaxResult getInfo(@PathVariable("administrativeId") Long administrativeId)
    {
        return success(sysAdministrativeService.selectSysAdministrativeByAdministrativeId(administrativeId));
    }

    /**
     * 新增行政区
     */
    @PreAuthorize("@ss.hasPermi('system:administrative:add')")
    @Log(title = "行政区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAdministrative sysAdministrative)
    {
        return toAjax(sysAdministrativeService.insertSysAdministrative(sysAdministrative));
    }

    /**
     * 修改行政区
     */
    @PreAuthorize("@ss.hasPermi('system:administrative:edit')")
    @Log(title = "行政区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAdministrative sysAdministrative)
    {
        return toAjax(sysAdministrativeService.updateSysAdministrative(sysAdministrative));
    }

    /**
     * 删除行政区
     */
    @PreAuthorize("@ss.hasPermi('system:administrative:remove')")
    @Log(title = "行政区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{administrativeIds}")
    public AjaxResult remove(@PathVariable Long[] administrativeIds)
    {
        return toAjax(sysAdministrativeService.deleteSysAdministrativeByAdministrativeIds(administrativeIds));
    }




}
